
/**
 * 在执行行复制之前执行该方法，如果
 * 返回false, 则不继续执行操作。
 */
function jkbxBeforeBoLineCopy(copyRows){ 
		var subDs = this.getNowSubDs();
		if(subDs.id != 'bx_jkbxitems' || subDs.id != 'jk_items')
			return;
		var index1= subDs.nameToIndex("pk_busitem");
		var index2 = subDs.nameToIndex("pk_jkbx");
		for(var i=0;i<copyRows.length;i++){
			copyRows[i].setCellValue(index1,null);
			copyRows[i].setCellValue(index2,null);  
		}
}; 

/*'新增'操作中的'复制新增'*/
function jkbxCopy()
{
   var headDs = this.getMasterDs();
   if(headDs == null)
     return;
   this.callAllDsClearUndo();
   
   isCopy = true;
   var selectRow = headDs.getSelectedRow();
    if(selectRow == null){
	showMessageDialog(webbx_trans('yer_jkbx005'));
	  	return;
   }
   // 增加新的主表行
   var newRow = selectRow.clone();
	/*过滤主表金额Map*/
	var ybArr = ['ybye','hkybje','zfybje','cjkybje'];
	var bbArr = ['bbye','hkbbje','zfbbje','cjkbbje'];	
	var ybMap = new HashMap();
 	var bbMap = new HashMap();
	for(var i in ybArr){
 		ybMap.put(ybArr[i], ybArr[i]);
 		bbMap.put(bbArr[i], bbArr[i]);
 	}
 	var meta = headDs.metadata;      
	//必须在加行之前执行
 	for(var t = 0, count = meta.length; t < count; t++){	
		if(meta[t].isForeignKey || meta[t].isPrimaryKey){
			newRow.setCellValue(t, null);
		}
	}
		
	//后面有用到此状态
	this.billUI.setBillOperate(IBillOperate.OP_ADD);
	//增行后，子ds对应的数据将都改变，所以需要提前记录
	var subDsArray = this.getAllSlaveDs();
	var subDsRowArray = new Array;
	if(subDsArray != null && subDsArray.length != 0)
	{
		var subDsRowsMap = new HashMap();
		for(var i = 0; i < subDsArray.length; i ++)
			subDsRowArray.push(subDsArray[i].getRows());
	}
	/**********************复制后对数据过滤**********************************/
	//主表中需清零的字段
 	var newRowIndex = headDs.addRow(newRow, true);
	for(var t = 0, count = meta.length; t < count; t++){	
		if(headDs.id == 'bx_bxzb' && ybMap.containsKey(meta[t].key)){
			headDs.setValueAt(newRowIndex, t, 0);
		}
	}
	//主表不能拷贝的字段
	var notCopys = ["jsh","djzt","spzt","ts","dr","pk_jkbx","djbh","kjnd","kjqj","jsrq",
					"shrq","contrastenddate","paydate","payflag"];
	var notCopys2 = ["lrr","jsr","payman","enduser","pk_item"];
	for(var i=0;i<notCopys.length;i++){
		var index = headDs.nameToIndex(notCopys[i]);
		if(index != -1)
			headDs.setValueAt(newRowIndex,index,null);
	}
	for(var i=0;i<notCopys2.length;i++){
		var index = headDs.nameToIndex(notCopys2[i]);
		if(index != -1)
			headDs.setValueAt(newRowIndex,index,null,true);//触发参照
	}
	//设置借款单的最后日期
	if(headDs.id == 'jk_jkzb'){
		var qcbz = headDs.getSelectedRow().getCellValue(headDs.nameToIndex('qcbz'));
		//如果借款单不是期初单据，则设置最后还款日期
		if(qcbz != 'Y'){
			var zhDate = headDs.metadata[headDs.nameToIndex("zhrq")].dftValue;
			headDs.setValueAt(newRowIndex,headDs.nameToIndex('zhrq'),zhDate);
		}
	}
	//拷贝时设置默认值
	$setDefaultValue(headDs,newRowIndex);
	//如果是從代辦事物過來的，則複製時刪掉報銷人
	var approveType = getParameter('approve_type');
	if(approveType != null && approveType == '1'){
		var pk_psndoc = getService("webbx_common_service").getPkPsndocByUser();
		var head_jkbxr = headDs.getSelectedRow().getCellValue(headDs.nameToIndex('jkbxr'));
		if(pk_psndoc != head_jkbxr)
			headDs.setValueAt(newRowIndex,headDs.nameToIndex('jkbxr'),null,true);
	}
	//如果是代理别人的单据要复制
	var jkbxr = headDs.getSelectedRow().getCellValue(headDs.nameToIndex('jkbxr'));
	if(jkbxr != null && jkbxr != ''){
		var djlxbm = headDs.getSelectedRow().getCellValue(headDs.nameToIndex('djlxbm'));
		var v = getService("webbx_common_service").getAgent(djlxbm,jkbxr);
		if(v != 'Y')
			headDs.setValueAt(newRowIndex,headDs.nameToIndex('jkbxr'),null,true);
	}
	/**********************************************************************/
	var bbhl_oldValue = newRow.getCellValue(headDs.nameToIndex("bbhl"));
	//如果是外币则需要重新触发币种字段
	var bzbm_index = headDs.nameToIndex("bzbm");
	var bzbm_value = newRow.getCellValue(bzbm_index);
		//后面有用到此状态
	this.billUI.setBillOperate(IBillOperate.OP_ADD);
	reset4Bzbm1(headDs,bzbm_value);
	headDs.setValueAt(newRowIndex,headDs.nameToIndex("bbhl"),bbhl_oldValue);
	
	var subDsArray = this.getAllSlaveDs();
	if(subDsArray != null && subDsArray.length != 0){
		try{
	    	for(var i = 0; i < subDsArray.length; i++){
	       		if(subDsArray[i] != null){    
	       			//不复制冲销表
					if(subDsArray[i].id == 'bx_contrast' || subDsArray[i].id == 'jk_bxcontrast')
			        	continue;
		        	else{ 
						var meta = subDsArray[i].metadata;           
	           			var rowCount = subDsRowArray[i].length, currRows = subDsRowArray[i];
	           			var newRowArr = new Array();
	           			
	        			// 复制当前keyValue的所有行
	        			var finybjeIndex = -1;
	        			var finbbjeIndex = -1;
	        			if(subDsArray[i].id == 'bx_fin'){
	        				finybjeIndex = subDsArray[i].nameToIndex('ybje');
	        				finbbjeIndex = subDsArray[i].nameToIndex('bbje');
	        			} 
	           			for(var k = 0; k < rowCount; k++){
	           				var row = currRows[k].clone();
				       		//是主键或外键，就置空
				       		for(var t = 0, count = meta.length; t < count; t++){ 
			       				if(meta[t].isForeignKey || meta[t].isPrimaryKey){
			       					row.setCellValue(t, null);
			      	 			}
			      	 			//财务页签的额外处理
			      	 			if(subDsArray[i].id == 'bx_fin' ){
			      	 				//将冲借款、还款的金额清零
			      	 				if(meta[t].key == 'cjkybje' || meta[t].key == 'hkybje' 
			      	 						|| meta[t].key == 'cjkbbje'|| meta[t].key == 'hkbbje')
			      	 					row.setCellValue(t,0)
			      	 				//支付=报销金额
			      	 				else if(meta[t].key == 'zfybje')
			      	 					row.setCellValue(t, row.getCellValue(finybjeIndex));
			      	 				else if(meta[t].key == 'zfbbje')
			      	 					row.setCellValue(t, row.getCellValue(finbbjeIndex));
			      	 				else if(meta[t].key == 'jkbxr')
			      	 					row.setCellValue(t, null);
			      	 			} 
							}
							newRowArr.push(row);
	           			}
	           			if(newRowArr.length > 0)
	           				subDsArray[i].insertRows(0, newRowArr, false);
	        		} 
	       		}
	    	}
    	}
    	catch(error){
    		isCopy = false;
    	}
  	}
   	this.onAfterBoCopyAdd();
   	isCopy = false;
   	return true;
};


function reset4Bzbm1(headDs,bzbm_value){
	var djlx =  (headDs.id == "bx_bxzb") ? "bx" : "jk";
    reset4Bzbm(headDs,bzbm_value,djlx);
}

function $setDefaultValue(headDs,newRowIndex){
	var qcbz = headDs.getRow(newRowIndex).getCellValue(headDs.nameToIndex('qcbz'));
	//期初单据
	if(qcbz == 'Y'){
		var date = getService("webbx_common_service").getDateBeforeQyrq(1);
		headDs.setValueAt(newRowIndex,headDs.nameToIndex('qcbz'),'Y');
		headDs.setValueAt(newRowIndex,headDs.nameToIndex('sxbz'),1);
		headDs.setValueAt(newRowIndex,headDs.nameToIndex('shr'),getPkUser(),true);
		headDs.setValueAt(newRowIndex,headDs.nameToIndex('shrq'),date);
		headDs.setValueAt(newRowIndex,headDs.nameToIndex('jsrq'),date);
		headDs.setValueAt(newRowIndex,headDs.nameToIndex('jsr'),getPkUser(),true);
		headDs.setValueAt(newRowIndex,headDs.nameToIndex('lrr'),getPkUser(),true);
		headDs.setValueAt(newRowIndex,headDs.nameToIndex('djrq'),date);
		headDs.setValueAt(newRowIndex,headDs.nameToIndex('djzt'),3);
	}else{
		headDs.setValueAt(newRowIndex,headDs.nameToIndex('qcbz'),'N');
		headDs.setValueAt(newRowIndex,headDs.nameToIndex('sxbz'),0);
		headDs.setValueAt(newRowIndex,headDs.nameToIndex('shr'),null,true);
		headDs.setValueAt(newRowIndex,headDs.nameToIndex('lrr'),getPkUser(),true);
		headDs.setValueAt(newRowIndex,headDs.nameToIndex('djrq'),getLoginDate());
	}
	
}
